package com.cn.wanxi.servlet.news;

import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.INewsService;
import com.cn.wanxi.service.impl.NewsServiceImpl;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/backNewsSelectFindAll")
public class NewsFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String isTop = req.getParameter("isTop");
        String status = req.getParameter("status");
//分页参数
        String startNum = req.getParameter("startNum");
        String pageSize = req.getParameter("pageSize");

        NewsModel newsModel = new NewsModel();
        newsModel.setTitle(title);
        newsModel.setContent(content);
        newsModel.setIsTop(isTop != null ? Integer.parseInt(isTop) : -1);
//        newsModel.setStatus("on".equals(status) ? 1 : 0);
        newsModel.setStatus(status != null ? Integer.parseInt(status) : -1);

        PageModel pageModel = new PageModel();
        pageModel.setStartNum(Integer.parseInt(startNum));
        pageModel.setPageSize(Integer.parseInt(pageSize));
//        1.从什么时候开始数，startNum 2.数多少条数据 每一页显示多少条 pageSize


        INewsService iNewsService = new NewsServiceImpl();
        ResultModel resultModel = iNewsService.findAll(newsModel, pageModel);

        JSONArray jsonArray = JSONArray.fromObject(resultModel);
        resp.getWriter().println(jsonArray);
    }
}
